Skip to content

drm/asahi: g16: add a recovery PB slot for a render accepted before a hang - #4240

Merged
DjDeveloperr merged 3 commits into
mainfrom
fix/g16-late-pb-spare
Dec 4, 2026
Merged

DjDeveloperr merged 3 commits into
mainfrom
fix/g16-late-pb-spare

Conversation

@DjDeveloperr

@DjDeveloperr DjDeveloperr commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

What: After one client's render hangs and the GPU recovers, another client's render that was already queued could be cancelled (ECANCELED, the client sees a lost device). The hang had quarantined the PB allocator slot its VM shared with the hung VM, and recovery slots were only added at SUBMIT. The engine now has a recovery slot added for such a render off the device lock and holds it meanwhile; it cancels only when no slot can be added. Refs #4032 (its hang rows found this).

Tested: J773g (Mac mini M4) and J516s (M3 Pro) rows PASS at 812188767bea (comments below): the queued client was cancelled in 4 of 6 and 3 of 6 rounds on base, 0 of 6 on both with the fix. Host: W=1 of drivers/gpu/drm/asahi builds with the same 42 warnings as main. tools/asahi/run-host-tests.py --only g16 4/4 and --only asahi-m4 1/1 in the pinned ubuntu 24.04 container (rust 1.96). The new g16-late-pb-spare-tests.py fails on main's code and kills 4/4 mutants.

Risk: two-way · M4 (T8132) and M3, M3 Pro, M3 Max (T8122, T6030, T6031) on 27.2 firmware, the Macs that use the g16 backend. Only a render left with no PB slot it can take after a GPU recovery goes the new way.

Not yet: SCS renders in that state are still cancelled: they use base slots only.

Details

How the render gets stranded. Each rendering VM gets two warm PB/UMA allocator slots [home, alt] at its first render (pick_base_pb_slots(next_pb_slot++)), so VM n holds {n, n+1} and the next VM's home is its alt. A render of that next VM that hangs on its home has its abort quarantine the slot for good (Pools::quarantine_flight). Every recovery calls Pools::invalidate(), so each VM's next render needs a fresh InitBM epoch, which may only go on a displaced slot. The queued VM's own slot was the last one the TA and FS engines ran, so it is not displaced, and its other slot is quarantined. The recovery slots (WARM_PB_SLOTS..PB_SLOTS) were added only in Cache::prepare(), and this render was prepared before the hang, so exhausted_for_mask_mode was true and the engine cancelled it. With many clients another render displaces the slot, so the case needs a lone client next to the hung one.

The change.

  • The VM's recovery slots move from Ready (its mutex is never taken by publication) to a spinlock on Cache that the engine may take. A prepared job keeps them in set-once cells instead of a snapshot.
  • When the drained engine finds no slot a render can take, Prepared::pb_stranded gives the job the recovery slots its VM got since the job was prepared. If there are none, it spawns a one-shot worker (system_unbound, the spawn itself GFP_NOWAIT) and the engine holds the render. The worker adds a slot under ready, through the same grow_pb_ext helper SUBMIT uses, and wakes publication.
  • The render is cancelled as before when no slot can be added (allocation failure, every recovery slot quarantined) or when it is an SCS render. One addition per VM is under way at a time.
  • A strand leaves the VM at most one healthy slot (of two healthy idle slots one is always displaced), so the worker grows to the same BASE_PB_SLOTS target as SUBMIT.

Why not a recovery slot for every VM up front. Each recovery slot carries a 32 MiB UMA pool, per VM, and SCS renders would still be exposed.

Fence path. The held render's fence now waits for one allocation, done in a worker outside the device lock and the fence signalling section, as G17 grows render PB memory in a worker today. The engine stops publishing for that one allocation, which only happens after a recovery.

No module parameter, runtime switch or SoC gate. G17 has its own render resource code and does not take this path.

Evidence
  • New host test tools/asahi/g16-late-pb-spare-tests.py: copies the slot picker, publication, retirement, quarantine and recovery bookkeeping from g16/pools.rs and the new g16/resources.rs code into a host build with mocks, and replays the hang next to a queued render (plus an SCS render, an allocation failure and every recovery slot quarantined). It also checks that publish_checked in engine.rs keeps the decision the test models.
    • this branch: PASS control, 4/4 mutants killed (cancel-stranded, no-adopt, failed-holds, respawn)
    • --root at main 7303d70: FAIL line 1: the render queued before the hang was cancelled
  • asahi-m4 harnesses: test-resources.py (102 passed) and test-publication.py (94 passed, new stranded_render_waits_for_its_recovery_slot) updated for the set-once cells and the hold.
  • W=1 drivers/gpu/drm/asahi/ (arm64, gcc + rustc) from a warm tree: OK on main and on this branch, identical 42 warnings, none in the changed code.

… hang

Each rendering VM gets two warm PB/UMA allocator slots, and the next
VM's home slot is its alternate. When a render of the next VM hangs on
that shared slot, its abort quarantines the slot for good, and the
recovery makes every VM start a fresh InitBM epoch, which may only go
on a displaced slot. A VM alone whose render was accepted before the
hang then has none: its own slot was the last one the TA and FS engines
ran, the other one is quarantined, and recovery slots
(WARM_PB_SLOTS..PB_SLOTS) were only added at SUBMIT. The engine
cancelled that render with ECANCELED and the innocent client saw a lost
device.

Keep the VM's recovery slots in its cache behind a spinlock the engine
may take, and give a prepared job set-once cells for them. When the
drained engine finds no slot a render can take, the job takes the
recovery slots its VM got since the job was prepared. If there are
none, a one-shot worker adds one off the device lock and wakes
publication while the engine holds the render. The render is cancelled
as before only when no slot can be added (allocation failure, every
recovery slot quarantined) or when it is an SCS render, which uses base
slots only.

A strand leaves the VM at most one healthy slot (of two healthy idle
slots one is always displaced), so the worker grows to the same
BASE_PB_SLOTS target as SUBMIT, through one shared helper.

Add tools/asahi/g16-late-pb-spare-tests.py: the production slot picker
and quarantine bookkeeping (pools.rs) and the new resources.rs code,
built against host mocks, replay the hang next to a queued render. It
fails on the previous engine decision and kills four mutants. Update
the asahi-m4 resources and publication harnesses for the set-once
cells and the hold.

Signed-off-by: DjDeveloperr <hey@djdev.me>
Cache::close() took the recovery PB slots in the tail expression of the
block that holds the ready lock, so the spinlock guard, a temporary of
that expression, outlived the ready guard. Take them in a statement of
their own, so the spinlock is released before the ready lock, in the
reverse order of taking them.

Signed-off-by: DjDeveloperr <hey@djdev.me>
Cache::add_late_pb() takes the ready lock, and a render the engine
holds for a recovery PB slot waits for that worker, so the comment in
Cache::prepare() that counts fence signalling among the paths that
never hold the lock is no longer true. Say instead that no GPU wait may
go under it: one would leave the held render, and publication with it,
waiting for itself.

The comment keeps its two lines, so the built module does not change.

Signed-off-by: DjDeveloperr <hey@djdev.me>

@DjDeveloperr DjDeveloperr left a comment •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One review round at 5a9369be0947, plus one small fix pushed on top (812188767bea, comment only).

Verdict: the fix is right and small. It can merge once the J773g and J516s rows pass (both module-only row kits are sealed at this head). This is a comment review: the PR is on my own account.

What I checked

  • Lock order. The new pb_ext spinlock is always taken last: under ready in prepare(), grow_pb_ext() and close(), and under the device lock in pb_stranded(). Nothing is locked, allocated or freed while it is held. The slot grow_pb_ext() fills is always empty because every writer holds ready, and the second commit makes close() release it before ready. The engine never takes ready, and close() is not called under the device lock. So no lock cycle.
  • Fence path. The engine side never sleeps or reclaims (try_spawn(GFP_NOWAIT), SetOnce::populate, an Arc clone). The worker's GFP_KERNEL allocation can't wait on the held render: the driver's only shrinker (VmShrinker) frees idle VMs through Gate::try_reclaim and never waits for the GPU.
  • Teardown. The worker owns an Arc<Cache> and an Arc<VmState>. close() sets closed before taking ready, and the worker checks closed, the VM status and is_closed() under ready. So a slot added just before a close is taken out and dropped by close(). A failed VM leaves the state at FAILED, and no render of that VM comes back to read it.
  • Second recovery while the worker runs. It can quarantine the slot just added. The render adopts that slot, is still stranded and asks again. Each round adds another of the four recovery slots, then the render is cancelled, so the loop ends.
  • Can a held render stall publication for good? A failed spawn, a failed allocation, a closed or failed VM, or all four recovery slots quarantined each end in the same cancel as on main. The worker only waits for one allocation and for ready, and ready is only held across allocations. The hold has no deadline of its own, and I don't think it needs one.
  • SCS. pb_stranded() returns ENOSPC before touching the state, so SCS renders are cancelled as on main.
  • Worse than main? Only renders that main would cancel take the new path. The cost is that publication stops behind the held render while the worker runs (the engine breaks on it), for every VM, not only the stranded one. That is one allocation, and only after a recovery, so it's fine.
  • Org rules: no module parameter, runtime switch or SoC gate. The LATE_PB_* constants are names for states.

Fixed in 812188767bea. prepare() said ready is never held by fence signalling. That stops being true here: a held render, and publication behind it, waits for add_late_pb(), which takes ready. A GPU wait added under ready later would now deadlock the device, so the comment says that instead. It keeps its two lines, so the built module is byte-identical to the one from 5a9369be0947 (checked on the J773g and J516s kernels).

Tests at 812188767bea (pinned ubuntu 24.04 container, rust 1.96):

  • g16-late-pb-spare-tests.py: PASS control, 4/4 mutants killed. With --root at main 7303d70ea68d it reports FAIL line 1, as the PR says.
  • run-host-tests.py --only g16: 4/4. --only asahi-m4: 1/1 (test-resources 102 passed, test-publication 94 passed).

Still owed before merge: the J773g row and the J516s row (the g16 backend is shared with M3). Both passed, see below.


Update 00:48Z, a second read by a reviewer that didn't push (same head 812188767bea). The rule is one review per PR, so this goes here and not in a new review.

  • Rows (comments above): J773g (Mac mini M4, G16G) PASS: base cancelled the queued client in 4 of 6 rounds, fix in 0 of 6, and the worker added the slot. J516s (M3 Pro, G15S) PASS: base 3 of 6, fix 6 of 6 with the strand hit in 4 rounds, WARN=0.

  • Code, read again without the notes above: the device lock is a mutex, so pb_ext is a leaf spinlock under it and under ready. Nothing is freed while it is held: the cache keeps its own reference, so an adopted clone is never the last one, and close() drops the taken slots after both locks. SetOnce drops what it holds, so an adopted slot goes away with its job. A new recovery slot has no owner, so the picker counts it as displaced and the held render can take it. Only close(), prepare() and the worker take ready, and none of them waits on the GPU under it. close() sets closed before it takes ready, so a late worker can't put a slot back. Only the g16 backend runs it (M4, and M3 on 27.2 firmware); the g15 and g17 backends don't.

  • Not blocking: if a held render's queue goes away before the engine reads a failed addition, the VM's next strand is cancelled without a new try. Main cancels it too, so this is no worse.

  • Local checks on the exact head (pc: merge base 7303d70e + this diff, the six changed files checked against the head's blob ids). The asahi crate type-checks with a warm J516s tree's own rustc line: 41 warnings on base and on head, the same set. With clippy-driver (what CLIPPY=1 runs) the set is also unchanged; one needless_range_loop moved with its loop. rustfmt finds nothing in the new code. In the pinned host-test container: run-host-tests.py --only g16 4/4, --only asahi-m4 1/1, g16-late-pb-spare-tests.py control PASS with 4/4 mutants killed, and it FAILs at the merge base.

  • CI: apple-build, host-tests, KUnit asahi, readiness and pr-text all pass at this head.

No changes needed. It's ready for an approval from someone who didn't push it.

@DjDeveloperr

Copy link
Copy Markdown
Contributor Author

J773g (Mac mini M4, T8132, G16G B0, OS fw 27.2) A/B hardware verification for head 812188767bea28526cb6f1c4eac50dd2ccf408d0: VERDICT PASS.

  • Kernel & kit: stock 7.3.0-j773g-main3685+ (Stage 2 1.0.0b18-g0745e344), work/j773g-g16-late-pb-spare-20261203 (SHA256SUMS c7cc00f07d97c517fc63087db64b3df986b4584fb4c331ce3adc63676d48b6c1, --check 9/9, check j773g 8/8; comment-only update to 812188767bea verified byte-identical to sealed aurora-base.ko d8062108 and aurora-fix.ko 30904c99)
  • Arm base (#4198, aurora.ko build ID 7132c4cb7959e4cd0d947dbf09799a5bf7a058e7, boot 22dee80f): reproduces case-2 ECANCELED on 4 of 6 rounds (R3–R6):
    • R1–R2: innocent_rc=0, ok=3000 bad=0 err=0, strand=0
    • R3–R6: innocent_rc=42, ok=908..911 err=1 lost=1, strand=2, latch_ecanceled=2, kmsg_ecanceled=1 per round; recoveries 6, WARN=0, after rc=0 ok=577 bad=0 err=0
  • Arm fix (#4240 812188767bea, aurora.ko build ID e131b5bc296e86d781ca55096f593f6b025292c3, boot b25c2699): 0 of 6 rounds cancelled the innocent client:
    • R1: innocent_rc=0 ok=2983 bad=0 err=0 lost=0, strand=0, latch_ecanceled=0 kmsg_ecanceled=0
    • R2: innocent_rc=0 ok=2999 bad=0 err=0 lost=0, strand=3 pb_stranded=3 add_late_pb=1 slots_added=1, latch_ecanceled=0 kmsg_ecanceled=0
    • R3: innocent_rc=0 ok=3000 bad=0 err=0 lost=0, strand=0 slots_added=1, latch_ecanceled=0 kmsg_ecanceled=0
    • R4: innocent_rc=0 ok=2998 bad=0 err=0 lost=0, strand=7 pb_stranded=7 add_late_pb=1 slots_added=1, latch_ecanceled=0 kmsg_ecanceled=0
    • R5: innocent_rc=0 ok=2998 bad=0 err=0 lost=0, strand=0 slots_added=1, latch_ecanceled=0 kmsg_ecanceled=0
    • R6: innocent_rc=0 ok=2913 bad=0 err=0 lost=0, strand=3 pb_stranded=3 add_late_pb=1 slots_added=2, latch_ecanceled=0 kmsg_ecanceled=0
    • Post-row: after rc=0 ok=577 bad=0 err=0, WARN=0, health: failed 0 errno 0 degraded 0, recoveries 6, hang_kills 0, class_unavailable=0

@DjDeveloperr

Copy link
Copy Markdown
Contributor Author

J516s (MacBook Pro 14" M3 Pro, apple,j516s, T6030, G15S B1, 18 active GPU cores, OS fw 27.2) A/B row for linux#4240 at head 812188767bea28526cb6f1c4eac50dd2ccf408d0 (module-only on stock 7.3.0-f3822, kit work/j516s-g16-late-pb-spare-20261203, SHA256SUMS ead0e6f6…):

  • Verdict: PASS (base reproduced the bug in 3/6 rounds; fix passed 6/6 rounds with 0 innocent cancellations while PB-slot stranding fired in 4/6 rounds)
  • Arm base (aurora-base.ko, build ID 6fbee5f78773b5f15fba76d268669abb3bdbab78, sha256 edb7fa85ec7f…, boot_id=7a41daf7-113a-4bf1-a921-b6b5402dc55b):
    • R1–R3: innocent ok=1131/1210/1235, bad=0, err=0, lost=0, strand=0, ECANCELED kmsg=0 latch=0
    • R4–R6 (reproduced): innocent cancelled rc=42, ok=369/367/367, bad=0, err=1, lost=1, strand=2, ECANCELED kmsg=1 latch=2 in all 3 rounds
    • Post-row: after rc=0 ok=239 bad=0 err=0, WARN=0, recoveries 6, hang_kills 0, health: failed 0 errno 0 degraded 0, class_unavailable=0
  • Arm fix (aurora-fix.ko, build ID 6ef2c9723f9efc61284bff55aff64d253f4521bb, sha256 f478d09f1775…, boot_id=d0ac625e-892b-4f46-afb8-aae0e341200c):
    • R1: innocent rc=0 ok=1131 bad=0 err=0 lost=0, strand=0, ECANCELED=0
    • R2: innocent rc=0 ok=1217 bad=0 err=0 lost=0, strand=6 pb_stranded=6 add_late_pb=1 slots_added=1, ECANCELED=0
    • R3: innocent rc=0 ok=1227 bad=0 err=0 lost=0, strand=4 pb_stranded=4 add_late_pb=1 slots_added=1, ECANCELED=0
    • R4: innocent rc=0 ok=1212 bad=0 err=0 lost=0, strand=0 slots_added=1, ECANCELED=0
    • R5: innocent rc=0 ok=1212 bad=0 err=0 lost=0, strand=4 pb_stranded=4 add_late_pb=1 slots_added=1, ECANCELED=0
    • R6: innocent rc=0 ok=1189 bad=0 err=0 lost=0, strand=4 pb_stranded=4 add_late_pb=1 slots_added=2, ECANCELED=0
    • Post-row: after rc=0 ok=239 bad=0 err=0, WARN=0, recoveries 6, hang_kills 0, health: failed 0 errno 0 degraded 0, class_unavailable=0
  • Stock return: cleanly returned to stock 7.3.0-f3822 + stock aurora.ko (d401ae24e2e09f18b73ed236a4a18f8504d1cbb0, boot_id=b596302f-9cf3-42e5-ba81-193339bc4ee7).
  • Evidence: work/j516s-g16-late-pb-spare-20261203/results/verdict.txt.

@DjDeveloperr
DjDeveloperr marked this pull request as ready for review December 4, 2026 00:49
@DjDeveloperr
DjDeveloperr requested review from a team as code owners December 4, 2026 00:49
@DjDeveloperr
DjDeveloperr merged commit cacc5d3 into main Dec 4, 2026
11 checks passed
@DjDeveloperr
DjDeveloperr deleted the fix/g16-late-pb-spare branch December 4, 2026 00:50
@DjDeveloperr

Copy link
Copy Markdown
Contributor Author

Merged at 812188767bea (main cacc5d311a9e). Admin merge because DjDeveloperr made the last push, so its approval can't count. Basis: independent review at this head (review above, update 00:48Z, no defects), J773g and J516s hardware rows PASS at this head, and apple-build, host-tests, KUnit asahi, readiness and pr-text all green.

DjDeveloperr added a commit that referenced this pull request Dec 4, 2026
Bring the branch up to main c042c5f. The merge is clean, and
nothing main changed in g16 since d1ae4f2 (#4198, #4240 and
others) touches the code this branch changes, so nothing was dropped
or adjusted.

Signed-off-by: DjDeveloperr <hey@djdev.me>
DjDeveloperr added a commit that referenced this pull request Dec 4, 2026
Bring the branch up to main c042c5f. The merge is clean, and
nothing main changed in g16 since d1ae4f2 (#4198, #4240 and
others) touches the code this branch changes, so nothing was dropped
or adjusted.

Signed-off-by: DjDeveloperr <hey@djdev.me>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant